<?php


namespace app\api\controller;

//http://cosmo.com/api/my/getOrderDetails
use app\api\model\Contrast;
use app\api\model\Member;
use app\api\model\Order;

class My extends BaseApi {

    public function getUserInfo(){
        if(!$this->request->param('openid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }
        $openid = trim($this->request->param('openid'));
        $member = new Member();
        $memberItem = $member->getMember($openid);

        if(!empty($memberItem)){
            $memberItem->agentName = $memberItem->agentid == 0 ? '卡斯摩' : $member->getMember($memberItem->agentid,'nickname')->nickname;
            return json(['code'=>1,'data'=>$this->getProportion($memberItem)]);
        }
        return json(['code'=>-1,'msg'=>'该用户不存在或已删除']);
    }
    //获取积分金额
    protected function getProportion($member){
        $contrast = new Contrast();
        $contrastItem = $contrast->find();
        $ratio = $contrastItem->ratio / $contrastItem->credit;
        $member->amount = $member->credit * $ratio;
        return $member;
    }

    //我的订单
    public function getOrderlist(){
        if(!$this->request->param('openid') || !$this->request->param('page')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $openid = trim($this->request->param('openid'));
        $page = max(1,intval($this->request->param('openid')));
        $pagesize = intval($this->request->param('pagesize'));

        $order = new Order();
        $orderList = $order->getOrderList($openid,$page,$pagesize);
        if($orderList->isEmpty()){

        }
        return json(['code'=>1,'data'=>$orderList]);
    }

    public function getOrderDetails(){
        if(!$this->request->param('orderid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $orderid = intval($this->request->param('orderid'));

        $order = new Order();
        $orderItem = $order->getDetails(['id'=>$orderid]);

        if(!empty($orderItem)){
            return json(['code'=>1,'data'=>$orderItem]);
        }
        return json(['code'=>-1,'msg'=>'订单不存在或已删除']);
    }
}



